<html>
<head><meta charset="utf-8"><title>What allows an enum nested in another enum to share niches? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html">What allows an enum nested in another enum to share niches?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232807497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232807497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232807497">(Apr 01 2021 at 18:59)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span><span class="p">;</span><span class="w"></span>

<span class="k">enum</span> <span class="nc">Reference</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Decimal</span><span class="p">(</span><span class="n">T</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Hex</span><span class="p">(</span><span class="n">T</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Named</span><span class="p">(</span><span class="n">T</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">enum</span> <span class="nc">Token</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">AttributeLiteral</span><span class="p">(</span><span class="n">T</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">AttributeReference</span><span class="p">(</span><span class="n">Reference</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="c1">// AttributeReference2(Reference&lt;T&gt;),</span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">dbg!</span><span class="p">(</span><span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">Token</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;&gt;</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>If you uncomment the variant the size goes from 1 to 2 — why is that? Why isn't it always 1 and 1 or 2 and 2?</p>
<p>Is it something like, when there's only one usage of <code>Reference</code> (or any nested enum), then the discriminants of <code>Token</code> are adjusted so that they are exclusive to <code>Reference</code>? Then once we get two usages of <code>Reference</code>, then you can't rely on that trick?</p>



<a name="232807801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232807801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232807801">(Apr 01 2021 at 19:01)</a>:</h4>
<p>I think that's true, yeah</p>



<a name="232808174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232808174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232808174">(Apr 01 2021 at 19:03)</a>:</h4>
<p>FWIW, your example with <code>Token&lt;bool&gt;</code> is 3 bytes either way</p>



<a name="232808307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232808307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232808307">(Apr 01 2021 at 19:04)</a>:</h4>
<p>so any layout tricks only seem to work for ZSTs in this case</p>



<a name="232808624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232808624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232808624">(Apr 01 2021 at 19:06)</a>:</h4>
<p>Oh, that’s unfortunate</p>



<a name="232808657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232808657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232808657">(Apr 01 2021 at 19:06)</a>:</h4>
<p>Or fortunate, I suppose.</p>



<a name="232808700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232808700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232808700">(Apr 01 2021 at 19:07)</a>:</h4>
<p>I think it's possible in theory that the single-ref version could be <code>(tag, bool)</code></p>



<a name="232808792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232808792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232808792">(Apr 01 2021 at 19:07)</a>:</h4>
<p>It means that there’s no space saving for manually flattening out an enum tree that has actual data in it</p>



<a name="232809030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232809030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232809030">(Apr 01 2021 at 19:09)</a>:</h4>
<p>I want that weird feature where a value can’t actually be referenced itself so it doesn’t need to be able to stand alone</p>



<a name="232809608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232809608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232809608">(Apr 01 2021 at 19:13)</a>:</h4>
<p>has that been seriously proposed? a bit like <code>packed</code>, I suppose.</p>



<a name="232810356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232810356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232810356">(Apr 01 2021 at 19:18)</a>:</h4>
<p>In various forms. The closest (but different) I can reference quickly is <a href="https://github.com/rust-lang/rfcs/pull/2593">https://github.com/rust-lang/rfcs/pull/2593</a></p>



<a name="232810375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232810375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232810375">(Apr 01 2021 at 19:18)</a>:</h4>
<p>And yeah packed is related</p>



<a name="232810626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232810626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232810626">(Apr 01 2021 at 19:20)</a>:</h4>
<p>My thinking is that because the value inside the enum needs to be valid in its own, it can’t be aggressively compacted. So, what would it look like to remove that limitation?</p>



<a name="232810984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232810984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232810984">(Apr 01 2021 at 19:22)</a>:</h4>
<p>Another way of thinking of it would be like extreme monomorphization where we get a new (incompatible) type</p>



<a name="232811497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232811497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232811497">(Apr 01 2021 at 19:26)</a>:</h4>
<p>Or a way of “overlapping” enum discriminants, but that touches on unsafe/UB things I believe.</p>



<a name="232828363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232828363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232828363">(Apr 01 2021 at 21:34)</a>:</h4>
<blockquote>
<p>It means that there’s no space saving for manually flattening out an enum tree that has actual data in it</p>
</blockquote>
<p>it might still be able to shuffle around variant fields to create a better niche. it can't do that for nested ones</p>



<a name="232839137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20allows%20an%20enum%20nested%20in%20another%20enum%20to%20share%20niches%3F/near/232839137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20allows.20an.20enum.20nested.20in.20another.20enum.20to.20share.20niches.3F.html#232839137">(Apr 01 2021 at 23:25)</a>:</h4>
<p>Ah yes; I think what I meant to say is that embedding one enum isn’t practically better than embedding two, even though the ZST example suggests it might be.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>